//
//  YTDelayView.m
//  YUNT
//
//  Created by 梁炜东 on 2017/11/21.
//  Copyright © 2017年 梁炜东. All rights reserved.
//

#import "YTDelayView.h"
#import "YTDelayCell.h"
#import "YTDelayHead.h"

@interface YTDelayView()<UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate, YTDelayCellDelegate>
@property(nonatomic, strong)UIView *bgView;
@property(nonatomic, strong)UIButton *closeBtn;
@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UITableView *myTableView;
@property(nonatomic, copy)YTDelayViewBlock block;
@end

@implementation YTDelayView

-(instancetype)initWithBlock:(YTDelayViewBlock)block
{
    self = [super init];
    if (self)
    {
        self.block = block;
        [self addSubview:self.bgView];
        [_bgView addSubview:self.titleLabel];
        [_bgView addSubview:self.closeBtn];
        [_bgView addSubview:self.myTableView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        tap.numberOfTapsRequired = 1;
        tap.numberOfTouchesRequired = 1;
        tap.delegate = self;
        [self addGestureRecognizer:tap];
    }
    return self;
}
-(void)closeAction:(UIButton *)sender
{
    self.block();
}
-(void)tapAction:(UITapGestureRecognizer *)tap
{
    self.block();
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    [self p_makeConstraints];
}
#pragma mark YTDelayCellDelegate
-(void)delegateRulerIndexPath:(NSIndexPath *)indexpath Value:(NSString *)value
{
    if (indexpath.section == 0)
    {
        publicModel.intervalTime = [value floatValue];
    }else
    {
        if (indexpath.row == 0)
        {
            publicModel.continueTime = [value floatValue];
        }
    }
    
}
-(void)delegateStartBtnAction:(UIButton *)sender
{
    if (publicModel.intervalTime <= 0){publicModel.intervalTime = 0.5;}
    if (publicModel.continueTime <= 0){publicModel.continueTime = 1;}
    [deviceManager takeDelayPhoto];
}
#pragma mark UITableViewDelegate UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 1;
    }else
    {
        return 4;
    }
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1 && indexPath.row == 3)
    {
        return 44;
    }else
    {
        return 50;
    }
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 28;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    YTDelayHead *head = [[YTDelayHead alloc] init];
    [head configObject:section];
    return head;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YTDelayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YTDelayCell" forIndexPath:indexPath];
    cell.delegate = self;
    cell.indexPath = indexPath;
    [cell configObject];
    return cell;
}
//设置head foot的背景色 必须通过下面这个代理
-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
//    view.tintColor = [UIColor clearColor];
    view.tintColor = YTColor(57, 57, 57);
}

-(void)p_makeConstraints
{
    if (SharedApplication.statusBarOrientation == UIInterfaceOrientationPortrait ||
        SharedApplication.statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {//竖屏
        [_bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
            make.height.mas_equalTo(ScreenWidth - 30);
            make.width.mas_equalTo(260);
        }];
    }else
    {//横屏
        [_bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.trailing.equalTo(self).offset(-100);
            make.height.mas_equalTo(ScreenHeight - 30);
            make.width.mas_equalTo(260);
        }];
    }
    [_titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(_bgView);
        make.height.mas_equalTo(39);
    }];
    [_closeBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.bottom.equalTo(_titleLabel);
        make.width.mas_equalTo(70);
    }];
    [_myTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.trailing.equalTo(_bgView);
        make.top.equalTo(_titleLabel.mas_bottom);
    }];
}
-(UIView *)bgView
{
    if (!_bgView)
    {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor clearColor];
        _bgView.layer.cornerRadius = 6;
        _bgView.layer.masksToBounds = YES;
    }
    return _bgView;
}
-(UIButton *)closeBtn
{
    if (!_closeBtn)
    {
        _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _closeBtn.backgroundColor = [UIColor clearColor];
        [_closeBtn setTitle:Language(@"关闭") forState:UIControlStateNormal];
        [_closeBtn setTitleColor:YTColor(92, 204, 225) forState:UIControlStateNormal];
        _closeBtn.titleLabel.font = [UIFont systemFontOfSize:17];
        [_closeBtn addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}
-(UILabel *)titleLabel
{
    if (!_titleLabel)
    {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.backgroundColor = [UIColor blackColor];
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.userInteractionEnabled = YES;
        _titleLabel.text = Language(@"延时摄影");
    }
    return _titleLabel;
}
-(UITableView *)myTableView
{
    if (!_myTableView)
    {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _myTableView.backgroundColor = [UIColor clearColor];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        [_myTableView registerClass:[YTDelayCell class] forCellReuseIdentifier:@"YTDelayCell"];
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableView.bounces = NO;
    }
    return _myTableView;
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"] ||
        [NSStringFromClass([touch.view class]) isEqualToString:@"UILabel"] ||
        [NSStringFromClass([touch.view class]) isEqualToString:@"UIButton"])
    {
        return NO;
    }
    return YES;
}
@end
